﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;

namespace Progrom
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
        }

        private void btnYes_Click(object sender, EventArgs e)
        {
            string userName = txtName.Text;
            string password = txtPwd.Text;

            string connString = @"server=localhost;User Id=root;password=123456;database=myschool;Character Set=utf8";
            MySqlConnection conn = new MySqlConnection(connString);

            //获取匹配账户的行号
            string sql = String.Format("select count(*) from User where UserName='{0}'and PassWord='{1}'", userName, password);

            try
            {
                conn.Open();
                MySqlCommand comm = new MySqlCommand(sql, conn);
                int n =Convert.ToInt32(comm.ExecuteScalar());//执行查询语句，返回匹配的行数
                if (n == 1)
                {
                    this.DialogResult = DialogResult.OK;
                    this.Tag = true;
                }
                else
                {
                    MessageBox.Show("您输入的用户名或密码错误！请重新输入", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.Tag = false;
                }
            }

            catch (Exception ex)    //捕获异常
            {
                MessageBox.Show(ex.Message, "数据库操作异常！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.Tag = false;
            }
            finally
            {
                conn.Close();       //关闭数据库链接
            }
           
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtPwd.Text = "";
            txtName.Focus();
        }
    }
}
